/*
 * aht20.c
 *
 *  Created on: Oct 25, 2025
 *      Author: AND
 */
#include "aht20.h"

/*
 * 1. Code1：
 * code.h 和code.c都建立好，把操作对象句柄拿到（包含相应的头文件）
 * 2. Code2:
 * 根据文档分析实现思路
 * （资料包:AHT20规格书）
 * 	 初始化：通过发送0x71获取一个字节的状态字，需要先发送吗？
 * 	 强转类型的时候。加的时候分别转
 * 	 主函数里接收温湿度数据_不太确定 msg类型 char
 * 	 HAL_UART_Transmit(&huart2, msg, strlen(msg), 100); 不确定发送数据多少
 * 	 -- 1. 将串口设为中断模式意义不大，因为此处没有从外设输入串口信息接收，而是展示数据 完成sprintf Transmit
 * 	 -- +1实现 +梳理 （蓝牙模块）
 */
#define AHT20_ADDRESS 0x70

void AHT20_Init()
{
	uint8_t readBuffer;
	HAL_Delay(40);
	HAL_I2C_Master_Receive(&hi2c1, AHT20_ADDRESS, &readBuffer, 1, HAL_MAX_DELAY);
	if((readBuffer & 0x08 )== 0x00)
	{
		// 发送
		uint8_t sendBuffer[3] = {0xBE,0x08,0x00};
		HAL_I2C_Master_Transmit(&hi2c1, AHT20_ADDRESS, sendBuffer, 3, 100);
	}
}
void AHT20_Read(float* ptemprature,float* phumidity)
{
	uint8_t sendBuffer[3] = {0xAC,0x33,0x00};
	uint8_t readBuffer[6];
	HAL_I2C_Master_Transmit(&hi2c1, AHT20_ADDRESS, sendBuffer, 3, 100);
	HAL_Delay(75);
	HAL_I2C_Master_Receive(&hi2c1, AHT20_ADDRESS, readBuffer, 6, 100);
	if((readBuffer[0] & 0x80) == 0)
	{
		uint32_t data_t,data_h;
		data_h = ((uint32_t)readBuffer[1]<<12)+((uint32_t)readBuffer[2]<<4)+((uint32_t)readBuffer[3]>>4);
		//data_t = ((uint32_t)readBuffer[3]<<16)+((uint32_t)readBuffer[4]<<8)+((uint32_t)readBuffer[5]);
		data_t = ((uint32_t)(readBuffer[3]&0x0F)<<16)+((uint32_t)readBuffer[4]<<8)+((uint32_t)readBuffer[5]);
		*ptemprature = data_t*200.0f/(1 << 20) - 50;
		*phumidity = data_h*100.0f/(1<<20);
	}
}

